package com.tadpole.music.view.fragment.study;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tadpole.music.R;
import com.tadpole.music.adapter.base.BaseAdapter;
import com.tadpole.music.adapter.study.ErrorTopicAdapter;
import com.tadpole.music.bean.study.ErrorQuestionFootBean;
import com.tadpole.music.bean.study.ErrorQuestionHeadBean;
import com.tadpole.music.bean.study.MoNiQuestionErrorBean;
import com.tadpole.music.config.Constant;
import com.tadpole.music.iView.study.ErrorQuestionIView;
import com.tadpole.music.iView.study.MoNiQuestionErrorIView;
import com.tadpole.music.presenter.study.ErrorQuestionPresenter;
import com.tadpole.music.presenter.study.MoNiQuestionErrorPresenter;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;
import com.tadpole.music.view.activity.login.LoginActivity;
import com.tadpole.music.view.activity.study.ErrorActivity;
import com.tadpole.music.view.activity.study.QuestionErrorActivity;
import com.tadpole.music.view.fragment.base.BaseFragment;
import com.tadpole.music.view.widget.MyRecyclerViewDivider;
import com.tadpole.music.view.widget.NiceImageView;

import java.util.List;
import java.util.Objects;

/**
 * 我的错题
 * Created By 孙岩
 * on 2020/9/10
 */
public class ErrorTopicFragment extends BaseFragment implements ErrorQuestionIView, MoNiQuestionErrorIView {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private static final String ARG_PARAM3 = "param3";
    //college_id
    private String mParam1;
    //name
    private String mParam2;
    //desc
    private String mParam3;
    //recyclerView
    private RecyclerView recyclerView;
    //刷新
    private SmartRefreshLayout rl_refresh;
    //头像
    private NiceImageView ivImage;
    //昵称
    private TextView tvName;
    //错题数
    private TextView tvCount;
    //adapter
    private ErrorTopicAdapter errorTopicAdapter;
    //desc
    private TextView tvDesc;
    //获取错题的presenter
    private ErrorQuestionPresenter errorQuestionPresenter;
    //下半部分
    private MoNiQuestionErrorPresenter moNiQuestionErrorPresenter;
    public static ErrorTopicFragment newInstance(String param1, String param2,String param3) {
        ErrorTopicFragment fragment = new ErrorTopicFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        args.putString(ARG_PARAM3, param3);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getContentViewId() {
        return R.layout.fragment_error_topic;
    }

    @Override
    protected void initAllMembersView(Bundle savedInstanceState) {
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
            mParam3 = getArguments().getString(ARG_PARAM3);
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void initView(View mRootView) {
        recyclerView = mRootView.findViewById(R.id.recyclerView);
        rl_refresh = mRootView.findViewById(R.id.rl_refresh);
        ivImage = mRootView.findViewById(R.id.ivImage);
        tvName = mRootView.findViewById(R.id.tvName);
        tvCount = mRootView.findViewById(R.id.tvCount);
        tvDesc = mRootView.findViewById(R.id.tvDesc);

        errorQuestionPresenter = new ErrorQuestionPresenter();
        errorQuestionPresenter.attachView(this);
        errorQuestionPresenter.errorQuestionHead(mParam1,1,"0");

        moNiQuestionErrorPresenter = new MoNiQuestionErrorPresenter();
        moNiQuestionErrorPresenter.attachView(this);
        moNiQuestionErrorPresenter.getMoNiError(mParam1, false,1,"");

        tvDesc.setText("说明:"+mParam2+mParam3);

        //添加刷新监听器
        rl_refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@io.reactivex.annotations.NonNull RefreshLayout refreshLayout) {
                moNiQuestionErrorPresenter.getMoNiError(mParam1, true,1,"");
            }
        });

        //上拉加载
        rl_refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@androidx.annotation.NonNull RefreshLayout refreshLayout) {
                moNiQuestionErrorPresenter.getMoNiErrorMore(mParam1,1,"");
            }
        });

    }

    @SuppressLint("SetTextI18n")
    @Override
    public void showErrorHead(ErrorQuestionHeadBean.DataBean bean) {
        RequestOptions requestOptions = new RequestOptions()
                .placeholder(R.mipmap.app_icon)
                .error(R.mipmap.app_icon)
                .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL);

        if (SpUtil.getInstance(getActivity()).getString(Constant.USER_AVATAR, "").equals("")) {
            Glide.with(Objects.requireNonNull(getActivity())).load(R.mipmap.app_icon).apply(requestOptions).into(ivImage);
        } else {
            Glide.with(Objects.requireNonNull(getActivity())).load(SpUtil.getInstance(getActivity()).getString(Constant.USER_AVATAR, "")).apply(requestOptions).into(ivImage);
        }

        if (SpUtil.getInstance(getActivity()).getString(Constant.USER_NICKNAME, "").equals("")) {
            tvName.setText("蝌蚪音基会员");
        } else {
            tvName.setText(SpUtil.getInstance(getActivity()).getString(Constant.USER_NICKNAME, ""));
        }

        tvCount.setText("全部错题：" + bean.getTotal() + "道");
    }


    @Override
    public void showQuestion(final List<MoNiQuestionErrorBean.DataBeanX.DataBean> list) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        //创建适配器
        errorTopicAdapter = new ErrorTopicAdapter(getActivity(),list);
        //给RecyclerView设置适配器
        recyclerView.setAdapter(errorTopicAdapter);

        errorTopicAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                if(Mutils.isNetworkAvailable()){
                    Intent intent = new Intent(getActivity(), ErrorActivity.class);
                    intent.putExtra("id", list.get(position).getId());
                    intent.putExtra("flag", "1");
                    startActivity(intent);
                }else{
                    ToastUtils.show("请检查您的网路设置");
                }
            }
        });
    }

    @Override
    public void notifyAdapter() {
        errorTopicAdapter.notifyDataSetChanged();
    }

    @Override
    public void stopRefresh() {
        rl_refresh.finishRefresh();
        rl_refresh.finishLoadMore();
    }

    @Override
    public void show401() {
        SpUtil.getInstance(getActivity()).putString(Constant.TOKEN, "");
        Intent intent = new Intent(getActivity(), LoginActivity.class);
        startActivity(intent);
    }
}
